package org.lcogt.tools.fitsviewer.server;

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;


public class JPGCreator {
	static Runtime r;
	//Static method to create jpgs for all fits files missing cooresponding jpgs in a directory
	public static Process createJPG(String jpgFileName,String fileName,String relativePath)
	{
		r = Runtime.getRuntime();
		File file = new File(jpgFileName.substring(0,jpgFileName.lastIndexOf("/")));
		//if the jpg folder is does not exist then it creates it
		if(!file.exists())
			createPath(jpgFileName.substring(0,jpgFileName.lastIndexOf("/")));
		//trys to create the jpg for the cooresponding fits file
		try {
			//gpp looses execute when packaged in a jar so needs to be chmod
			r.exec("chmod a+x "+ relativePath + "/gpp");
			Process p=r.exec(relativePath+"/gpp "+ fileName + " " +jpgFileName + " 2 " + relativePath+"/gpp_cfg.txt");
			System.out.println("./gpp "+ fileName + " " +jpgFileName + " 2 gpp_cfg.txt");
			return p;
			} catch (IOException e) {
				e.printStackTrace();
				
			}
		return null;
	}
	//creates path
	public static void createPath(String filePath)
	{
		try {
			r.exec("mkdir -p " + filePath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

